<?php

/*
* PHPSDL - Testing sample for Joystick support
*
* The most relevant parts are based on the testjoystick.c source code from
* the SDL distribution.
*
* Markus Fischer <mfischer@guru.josefine.at>
*
*/

set_time_limit(0);
dl("php_sdl.dll");

error_reporting(E_ALL);
//sdl_expose_internal_errors(true);
SDL_Init(SDL_INIT_JOYSTICK);

if ($argc > 1 && preg_match('/^\d+$/', $argv[1])) {
	if (!($screen = SDL_SetVideoMode(320, 200, 0, 0)))
		exit;
	if (false == ($joystick = SDL_JoystickOpen($argv[1])))
		exit;
	echo "Watching Joystick $argv[1]: " . SDL_JoystickName($argv[1]) . "\n";
	printf("Joystick has %d axes, %d hats, %d balls and %d buttons\n",
		   SDL_JoystickNumAxes($joystick), SDL_JoystickNumHats($joystick),
		   SDL_JoystickNumBalls($joystick), SDL_JoystickNumButtons($joystick));
	$done = false;
	while (!$done) {
		while ($val = SDL_PollEvent(&$event)) {
			switch ($event['type']) {
				case SDL_JOYAXISMOTION:
					printf("Joystick %d axis %d value: %d\n",
						$event['jaxis']['which'],
						$event['jaxis']['axis'],
						$event['jaxis']['value']);
					break;
				case SDL_JOYHATMOTION:
					printf("Joystick %d hat %d value: ",
						$event['jhat']['which'],
						$event['jhat']['hat']);
					if ($event['jhat']['value'] == SDL_HAT_CENTERED) {
						echo "centered";
					}
					if ($event['jhat']['value'] & SDL_HAT_UP) {
						echo "up";
					}
					if ($event['jhat']['value'] & SDL_HAT_RIGHT) {
						echo "right";
					}
					if ($event['jhat']['value'] & SDL_HAT_DOWN) {
						echo "down";
						}
						if ($event['jhat']['value'] & SDL_HAT_LEFT) {
							echo "left";
						}
						echo "\n";
						break;
				case SDL_JOYBALLMOTION:
					printf("Joystick %d ball %d delta: (%d,%d)\n",
						$event['jball']['which'],
						$event['jball']['ball'],
						$event['jball']['xrel'],
						$event['jball']['yrel']);
					break;
				case SDL_JOYBUTTONDOWN:
					printf("Joystick %d button %d down\n",
						$event['jbutton']['which'],
						$event['jbutton']['button']);
					break;
				case SDL_JOYBUTTONUP:
					printf("Joystick %d button %d up\n",
						$event['jbutton']['which'],
						$event['jbutton']['button']);
					break;
				case SDL_KEYDOWN:
					if ($event['key']['keysym']['sym'] != SDLK_ESCAPE)
						break;
					/* Fall through to signal quit */
				case SDL_QUIT:
						$done = true;
				}
			}
		}
} else {
	if (0 == $numjoysticks = sdl_numjoysticks()) {
		echo "No joysticks found\n";
		exit;
	}

	for ($i = 0; $i < $numjoysticks; $i++) {
		echo "Joystick $i: " . SDL_JoystickName($i) . "\n";
		if ($joystick = SDL_JoystickOpen($i)) {
			echo "Number of Axes:    " . SDL_JoystickNumAxes($joystick) . "\n";
			echo "Number of Balls:   " . SDL_JoystickNumBalls($joystick) . "\n";
			echo "Number of Hats:    " . SDL_JoystickNumHats($joystick) . "\n";
			echo "Number of Buttons: " . SDL_JoystickNumButtons($joystick) . "\n";
		}
		echo "\n";
	}
	echo wordwrap("This is a list of all Joysticks found by the SDL library. You can test a particular Joystick by passing its number as first argument.");
}

?>
